home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / u_man / cat1 / perlmod.Z / perlmod
Encoding:
Text File  |  1998-10-28  |  20.9 KB  |  595 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))        PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       perlmod - Perl modules (packages and symbol tables)
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.       PPPPaaaacccckkkkaaaaggggeeeessss
  13.  
  14.       Perl provides    a mechanism for    alternative namespaces to
  15.       protect packages from    stomping on each other's variables.
  16.       In fact, there's really no such thing    as a global variable
  17.       in Perl (although some identifiers default to    the main
  18.       package instead of the current one).    The package statement
  19.       declares the compilation unit    as being in the    given
  20.       namespace.  The scope    of the package declaration is from the
  21.       declaration itself through the end of    the enclosing block,
  22.       eval,    sub, or    end of file, whichever comes first (the    same
  23.       scope    as the _m_y() and    _l_o_c_a_l()    operators).  All further
  24.       unqualified dynamic identifiers will be in this namespace.
  25.       A package statement only affects dynamic variables--
  26.       including those you've used _l_o_c_a_l() on--but _n_o_t lexical
  27.       variables created with _m_y().    Typically it would be the
  28.       first    declaration in a file to be included by    the require or
  29.       use operator.     You can switch    into a package in more than
  30.       one place; it    merely influences which    symbol table is    used
  31.       by the compiler for the rest of that block.  You can refer
  32.       to variables and filehandles in other    packages by prefixing
  33.       the identifier with the package name and a double colon:
  34.       $Package::Variable.  If the package name is null, the    main
  35.       package is assumed.  That is,    $::sail    is equivalent to
  36.       $main::sail.
  37.  
  38.       The old package delimiter was    a single quote,    but double
  39.       colon    is now the preferred delimiter,    in part    because    it's
  40.       more readable    to humans, and in part because it's more
  41.       readable to eeeemmmmaaaaccccssss macros.  It    also makes C++ programmers
  42.       feel like they know what's going on--as opposed to using the
  43.       single quote as separator, which was there to    make Ada
  44.       programmers feel like    they knew what's going on.  Because
  45.       the old-fashioned syntax is still supported for backwards
  46.       compatibility, if you    try to use a string like "This is
  47.       $owner's house", you'll be accessing $owner::s; that is, the
  48.       $s variable in package owner,    which is probably not what you
  49.       meant.  Use braces to    disambiguate, as in "This is
  50.       ${owner}'s house".
  51.  
  52.       Packages may be nested inside    other packages:
  53.       $OUTER::INNER::var.  This implies nothing about the order of
  54.       name lookups,    however.  All symbols are either local to the
  55.       current package, or must be fully qualified from the outer
  56.       package name down.  For instance, there is nowhere within
  57.       package OUTER    that $INNER::var refers    to $OUTER::INNER::var.
  58.       It would treat package INNER as a totally separate global
  59.       package.
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))        PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))
  71.  
  72.  
  73.  
  74.       Only identifiers starting with letters (or underscore) are
  75.       stored in a package's    symbol table.  All other symbols are
  76.       kept in package main,    including all of the punctuation
  77.       variables like $_.  In addition, when    unqualified, the
  78.       identifiers STDIN, STDOUT, STDERR, ARGV, ARGVOUT, ENV, INC,
  79.       and SIG are forced to    be in package main, even when used for
  80.       other    purposes than their builtin one.  Note also that, if
  81.       you have a package called m, s, or y,    then you can't use the
  82.       qualified form of an identifier because it will be
  83.       interpreted instead as a pattern match, a substitution, or a
  84.       transliteration.
  85.  
  86.       (Variables beginning with underscore used to be forced into
  87.       package main,    but we decided it was more useful for package
  88.       writers to be    able to    use leading underscore to indicate
  89.       private variables and    method names.  $_ is still global
  90.       though.)
  91.  
  92.       _E_v_a_l()ed strings are compiled    in the package in which    the
  93.       _e_v_a_l() was compiled.    (Assignments to    $SIG{},    however,
  94.       assume the signal handler specified is in the    main package.
  95.       Qualify the signal handler name if you wish to have a    signal
  96.       handler in a package.)  For an example, examine _p_e_r_l_d_b._p_l in
  97.       the Perl library.  It    initially switches to the DB package
  98.       so that the debugger doesn't interfere with variables    in the
  99.       script you are trying    to debug.  At various points, however,
  100.       it temporarily switches back to the main package to evaluate
  101.       various expressions in the context of    the main package (or
  102.       wherever you came from).  See    the _p_e_r_l_d_e_b_u_g manpage.
  103.  
  104.       The special symbol __PACKAGE__ contains the current package,
  105.       but cannot (easily) be used to construct variables.
  106.  
  107.       See the _p_e_r_l_s_u_b manpage for other scoping issues related to
  108.       _m_y() and _l_o_c_a_l(), and    the _p_e_r_l_r_e_f manpage regarding
  109.       closures.
  110.  
  111.       SSSSyyyymmmmbbbboooollll TTTTaaaabbbblllleeeessss
  112.  
  113.       The symbol table for a package happens to be stored in the
  114.       hash of that name with two colons appended.  The main    symbol
  115.       table's name is thus %main::,    or %:: for short.  Likewise
  116.       symbol table for the nested package mentioned    earlier    is
  117.       named    %OUTER::INNER::.
  118.  
  119.       The value in each entry of the hash is what you are
  120.       referring to when you    use the    *name typeglob notation.  In
  121.       fact,    the following have the same effect, though the first
  122.       is more efficient because it does the    symbol table lookups
  123.       at compile time:
  124.  
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))        PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))
  137.  
  138.  
  139.  
  140.           local *main::foo      = *main::bar;
  141.           local $main::{foo}  = $main::{bar};
  142.  
  143.       You can use this to print out    all the    variables in a
  144.       package, for instance.  The standard _d_u_m_p_v_a_r._p_l library and
  145.       the CPAN module Devel::Symdump make use of this.
  146.  
  147.       Assignment to    a typeglob performs an aliasing    operation,
  148.       i.e.,
  149.  
  150.           *dick = *richard;
  151.  
  152.       causes variables, subroutines, formats, and file and
  153.       directory handles accessible via the identifier richard also
  154.       to be    accessible via the identifier dick.  If    you want to
  155.       alias    only a particular variable or subroutine, you can
  156.       assign a reference instead:
  157.  
  158.           *dick = \$richard;
  159.  
  160.       Which    makes $richard and $dick the same variable, but    leaves
  161.       @richard and @dick as    separate arrays.  Tricky, eh?
  162.  
  163.       This mechanism may be    used to    pass and return    cheap
  164.       references into or from subroutines if you won't want    to
  165.       copy the whole thing.     It only works when assigning to
  166.       dynamic variables, not lexicals.
  167.  
  168.           %some_hash = ();              # can't be my()
  169.           *some_hash = fn( \%another_hash );
  170.           sub fn {
  171.           local    *hashsym = shift;
  172.           # now    use %hashsym normally, and you
  173.           # will affect    the caller's %another_hash
  174.           my %nhash = (); # do what you    want
  175.           return \%nhash;
  176.           }
  177.  
  178.       On return, the reference will    overwrite the hash slot    in the
  179.       symbol table specified by the    *some_hash typeglob.  This is
  180.       a somewhat tricky way    of passing around references cheaply
  181.       when you won't want to have to remember to dereference
  182.       variables explicitly.
  183.  
  184.       Another use of symbol    tables is for making "constant"
  185.       scalars.
  186.  
  187.           *PI = \3.14159265358979;
  188.  
  189.       Now you cannot alter $PI, which is probably a    good thing all
  190.       in all.  This    isn't the same as a constant subroutine, which
  191.       is subject to    optimization at    compile-time.  This isn't.  A
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))        PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))
  203.  
  204.  
  205.  
  206.       constant subroutine is one prototyped    to take    no arguments
  207.       and to return    a constant expression.    See the    _p_e_r_l_s_u_b
  208.       manpage for details on these.     The use constant pragma is a
  209.       convenient shorthand for these.
  210.  
  211.       You can say *foo{PACKAGE} and    *foo{NAME} to find out what
  212.       name and package the *foo symbol table entry comes from.
  213.       This may be useful in    a subroutine that gets passed
  214.       typeglobs as arguments:
  215.  
  216.           sub identify_typeglob {
  217.           my $glob = shift;
  218.           print    'You gave me ',    *{$glob}{PACKAGE}, '::', *{$glob}{NAME}, "\n";
  219.           }
  220.           identify_typeglob    *foo;
  221.           identify_typeglob    *bar::baz;
  222.  
  223.       This prints
  224.  
  225.           You gave me main::foo
  226.           You gave me bar::baz
  227.  
  228.       The *foo{THING} notation can also be used to obtain
  229.       references to    the individual elements    of *foo, see the
  230.       _p_e_r_l_r_e_f manpage.
  231.  
  232.       PPPPaaaacccckkkkaaaaggggeeee CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrrssss aaaannnndddd DDDDeeeessssttttrrrruuuuccccttttoooorrrrssss
  233.  
  234.       There    are two    special    subroutine definitions that function
  235.       as package constructors and destructors.  These are the
  236.       BEGIN    and END    routines.  The sub is optional for these
  237.       routines.
  238.  
  239.       A BEGIN subroutine is    executed as soon as possible, that is,
  240.       the moment it    is completely defined, even before the rest of
  241.       the containing file is parsed.  You may have multiple    BEGIN
  242.       blocks within    a file--they will execute in order of
  243.       definition.  Because a BEGIN block executes immediately, it
  244.       can pull in definitions of subroutines and such from other
  245.       files    in time    to be visible to the rest of the file.    Once a
  246.       BEGIN    has run, it is immediately undefined and any code it
  247.       used is returned to Perl's memory pool.  This    means you
  248.       can't    ever explicitly    call a BEGIN.
  249.  
  250.       An END subroutine is executed    as late    as possible, that is,
  251.       when the interpreter is being    exited,    even if    it is exiting
  252.       as a result of a _d_i_e() function.  (But not if    it's
  253.       polymorphing into another program via    exec, or being blown
  254.       out of the water by a    signal--you have to trap that yourself
  255.       (if you can).)  You may have multiple    END blocks within a
  256.       file--they will execute in reverse order of definition; that
  257.       is:  last in,    first out (LIFO).
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))        PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))
  269.  
  270.  
  271.  
  272.       Inside an END    subroutine, $? contains    the value that the
  273.       script is going to pass to exit().  You can modify $?    to
  274.       change the exit value    of the script.    Beware of changing $?
  275.       by accident (e.g. by running something via system).
  276.  
  277.       Note that when you use the ----nnnn    and ----pppp switches    to Perl, BEGIN
  278.       and END work just as they do in aaaawwwwkkkk, as a degenerate case.
  279.       As currently implemented (and    subject    to change, since its
  280.       inconvenient at best), both BEGIN _a_n_d    END blocks are run
  281.       when you use the ----cccc switch for a compile-only    syntax check,
  282.       although your    main code is not.
  283.  
  284.       PPPPeeeerrrrllll CCCCllllaaaasssssssseeeessss
  285.  
  286.       There    is no special class syntax in Perl, but    a package may
  287.       function as a    class if it provides subroutines to act    as
  288.       methods.  Such a package may also derive some    of its methods
  289.       from another class (package) by listing the other package
  290.       name in its global @ISA array    (which must be a package
  291.       global, not a    lexical).
  292.  
  293.       For more on this, see    the _p_e_r_l_t_o_o_t manpage and the _p_e_r_l_o_b_j
  294.       manpage.
  295.  
  296.       PPPPeeeerrrrllll MMMMoooodddduuuulllleeeessss
  297.  
  298.       A module is just a package that is defined in    a library file
  299.       of the same name, and    is designed to be reusable.  It    may do
  300.       this by providing a mechanism    for exporting some of its
  301.       symbols into the symbol table    of any package using it.  Or
  302.       it may function as a class definition    and make its semantics
  303.       available implicitly through method calls on the class and
  304.       its objects, without explicit    exportation of any symbols.
  305.       Or it    can do a little    of both.
  306.  
  307.       For example, to start    a normal module    called Some::Module,
  308.       create a file    called Some/Module.pm and start    with this
  309.       template:
  310.  
  311.           package Some::Module;  # assumes Some/Module.pm
  312.  
  313.           use strict;
  314.  
  315.           BEGIN {
  316.           use Exporter     ();
  317.           use vars     qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  318.  
  319.           # set    the version for    version    checking
  320.           $VERSION     = 1.00;
  321.           # if using RCS/CVS, this may be preferred
  322.           $VERSION = do    { my @r    = (q$Revision: 2.21 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for    MakeMaker
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))        PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))
  335.  
  336.  
  337.  
  338.           @ISA           = qw(Exporter);
  339.           @EXPORT      = qw(&func1 &func2 &func4);
  340.           %EXPORT_TAGS = ( );      # eg:    TAG => [ qw!name1 name2! ],
  341.  
  342.           # your exported package globals go here,
  343.           # as well as any optionally exported functions
  344.           @EXPORT_OK   = qw($Var1 %Hashit &func3);
  345.           }
  346.           use vars        @EXPORT_OK;
  347.  
  348.           #    non-exported package globals go    here
  349.           use vars        qw(@more $stuff);
  350.  
  351.           #    initalize package globals, first exported ones
  352.           $Var1   =    '';
  353.           %Hashit =    ();
  354.  
  355.           #    then the others    (which are still accessible as $Some::Module::stuff)
  356.           $stuff  =    '';
  357.           @more   =    ();
  358.  
  359.           #    all file-scoped    lexicals must be created before
  360.           #    the functions below that use them.
  361.  
  362.           #    file-private lexicals go here
  363.           my $priv_var    =    '';
  364.           my %secret_hash =    ();
  365.  
  366.           #    here's a file-private function as a closure,
  367.           #    callable as &$priv_func;  it cannot be prototyped.
  368.           my $priv_func = sub {
  369.           # stuff goes here.
  370.           };
  371.  
  372.           #    make all your functions, whether exported or not;
  373.           #    remember to put    something interesting in the {}    stubs
  374.           sub func1         {}       # no    prototype
  375.           sub func2()    {}       # proto'd void
  376.           sub func3($$)  {}       # proto'd to    2 scalars
  377.  
  378.           #    this one isn't exported, but could be called!
  379.           sub func4(\%)  {}       # proto'd to    1 hash ref
  380.  
  381.           END { }        # module clean-up code here    (global    destructor)
  382.  
  383.       Then go on to    declare    and use    your variables in functions
  384.       without any qualifications.  See the _E_x_p_o_r_t_e_r    manpage    and
  385.       the the _p_e_r_l_m_o_d_l_i_b manpage for details on mechanics and
  386.       style    issues in module creation.
  387.  
  388.       Perl modules are included into your program by saying
  389.  
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))        PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))
  401.  
  402.  
  403.  
  404.           use Module;
  405.  
  406.       or
  407.  
  408.           use Module LIST;
  409.  
  410.       This is exactly equivalent to
  411.  
  412.           BEGIN { require Module; import Module; }
  413.  
  414.       or
  415.  
  416.           BEGIN { require Module; import Module LIST; }
  417.  
  418.       As a special case
  419.  
  420.           use Module ();
  421.  
  422.       is exactly equivalent    to
  423.  
  424.           BEGIN { require Module; }
  425.  
  426.       All Perl module files    have the extension ._p_m.     use assumes
  427.       this so that you don't have to spell out "_M_o_d_u_l_e._p_m" in
  428.       quotes.  This    also helps to differentiate new    modules    from
  429.       old ._p_l and ._p_h files.  Module names are also    capitalized
  430.       unless they're functioning as    pragmas, "Pragmas" are in
  431.       effect compiler directives, and are sometimes    called
  432.       "pragmatic modules" (or even "pragmata" if you're a
  433.       classicist).
  434.  
  435.       The two statements:
  436.  
  437.           require SomeModule;
  438.           require "SomeModule.pm";
  439.  
  440.       differ from each other in two    ways.  In the first case, any
  441.       double colons    in the module name, such as Some::Module, are
  442.       translated into your system's    directory separator, usually
  443.       "/".     The second case does not, and would have to be
  444.       specified literally.    The other difference is    that seeing
  445.       the first require clues in the compiler that uses of
  446.       indirect object notation involving "SomeModule", as in $ob =
  447.       purge    SomeModule, are    method calls, not function calls.
  448.       (Yes,    this really can    make a difference.)
  449.  
  450.       Because the use statement implies a BEGIN block, the
  451.       importation of semantics happens at the moment the use
  452.       statement is compiled, before    the rest of the    file is
  453.       compiled.  This is how it is able to function    as a pragma
  454.       mechanism, and also how modules are able to declare
  455.       subroutines that are then visible as list operators for the
  456.  
  457.  
  458.  
  459.      Page 7                        (printed 10/23/98)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))        PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))
  467.  
  468.  
  469.  
  470.       rest of the current file.  This will not work    if you use
  471.       require instead of use.  With    require    you can    get into this
  472.       problem:
  473.  
  474.           require Cwd;          # make Cwd:: accessible
  475.           $here = Cwd::getcwd();
  476.  
  477.           use Cwd;              # import names from Cwd::
  478.           $here = getcwd();
  479.  
  480.           require Cwd;          # make Cwd:: accessible
  481.           $here = getcwd();          # oops! no main::getcwd()
  482.  
  483.       In general, use Module () is recommended over    require
  484.       Module, because it determines    module availability at compile
  485.       time,    not in the middle of your program's execution.    An
  486.       exception would be if    two modules each tried to use each
  487.       other, and each also called a    function from that other
  488.       module.  In that case, it's easy to use requires instead.
  489.  
  490.       Perl packages    may be nested inside other package names, so
  491.       we can have package names containing ::.  But    if we used
  492.       that package name directly as    a filename it would makes for
  493.       unwieldy or impossible filenames on some systems.
  494.       Therefore, if    a module's name    is, say, Text::Soundex,    then
  495.       its definition is actually found in the library file
  496.       _T_e_x_t/_S_o_u_n_d_e_x._p_m.
  497.  
  498.       Perl modules always have a ._p_m file, but there may also be
  499.       dynamically linked executables or autoloaded subroutine
  500.       definitions associated with the module.  If so, these    will
  501.       be entirely transparent to the user of the module.  It is
  502.       the responsibility of    the ._p_m    file to    load (or arrange to
  503.       autoload) any    additional functionality.  The POSIX module
  504.       happens to do    both dynamic loading and autoloading, but the
  505.       user can say just use    POSIX to get it    all.
  506.  
  507.       For more information on writing extension modules, see the
  508.       _p_e_r_l_x_s_t_u_t manpage and    the _p_e_r_l_g_u_t_s manpage.
  509.  
  510.      SSSSEEEEEEEE AAAALLLLSSSSOOOO
  511.       See the _p_e_r_l_m_o_d_l_i_b manpage for general style issues related
  512.       to building Perl modules and classes as well as descriptions
  513.       of the standard library and CPAN, the    _E_x_p_o_r_t_e_r manpage for
  514.       how Perl's standard import/export mechanism works, the
  515.       _p_e_r_l_t_o_o_t manpage for an in-depth tutorial on creating
  516.       classes, the _p_e_r_l_o_b_j manpage for a hard-core reference
  517.       document on objects, and the _p_e_r_l_s_u_b manpage for an
  518.       explanation of functions and scoping.
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.      Page 8                        (printed 10/23/98)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))         22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))        PPPPEEEERRRRLLLLMMMMOOOODDDD((((1111))))
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.      Page 9                        (printed 10/23/98)
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.